﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace SoufunData
{
    public partial class LandInfoListForm : DevExpress.XtraEditors.XtraForm
    {
        public LandInfoListForm()
        {
            InitializeComponent();
        }

        private void LandInfoListForm_Load(object sender, EventArgs e)
        {
            dateEdit_CtimeStart.DateTime = DateTime.Now.AddYears(-3);
            dateEdit_CtimeEnd.DateTime = DateTime.Now;
            dateEdit_EndTimeS.DateTime = DateTime.Now.AddYears(-3);
            dateEdit_EndTimeE.DateTime = DateTime.Now;
            dateEdit_StartTimeS.DateTime = DateTime.Now.AddYears(-3);
            dateEdit_StartTimeE.DateTime = DateTime.Now;

            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }


        private DataTable _dtResult;
        private readonly SqlClass _sqlClass = new SqlClass();
 

        private void LoatData()
        {
            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("t.CTime between '");
            strWhere.Append(dateEdit_CtimeStart.DateTime);
            strWhere.Append("' and '");
            strWhere.Append(dateEdit_CtimeEnd.DateTime);
            strWhere.Append("' and t.StartTime between '");
            strWhere.Append(dateEdit_StartTimeS.DateTime);
            strWhere.Append("' and '");
            strWhere.Append(dateEdit_StartTimeE.DateTime);
            strWhere.Append("' and t.EndTime between '");
            strWhere.Append(dateEdit_EndTimeS.DateTime);
            strWhere.Append("' and '");
            strWhere.Append(dateEdit_EndTimeE.DateTime);
            strWhere.Append("' ");

            if (textEdit_LandParcelID.Text.Trim()!="")
            {
                strWhere.Append(" and t.LandParcelID like '%");
                strWhere.Append(textEdit_LandParcelID.Text.Trim());
                strWhere.Append("%' ");
            }
            if (textEdit_Vendee.Text.Trim() != "")
            {
                strWhere.Append(" and Vendee like '%");
                strWhere.Append(textEdit_Vendee.Text.Trim());
                strWhere.Append("%' ");
            }
            if (textEdit_Group.Text.Trim() != "")
            {
                strWhere.Append(" and `Group` like '%");
                strWhere.Append(textEdit_Group.Text.Trim());
                strWhere.Append("%' ");
            }
            if (imgeCombo_State.SelectedIndex!=0)
            {
                strWhere.Append(" and t.State ='");
                strWhere.Append(imgeCombo_State.SelectedItem);
                strWhere.Append("' ");
            }
            if (imgComboBox_PointsLoop.SelectedIndex!=0)
            {
                strWhere.Append(" and t.PointsLoop ='");
                strWhere.Append(imgComboBox_PointsLoop.SelectedItem);
                strWhere.Append("' ");
            }
            if (imgComBox_Board.SelectedIndex != 0)
            {
                strWhere.Append(" and t.Board ='");
                strWhere.Append(imgComBox_Board.SelectedItem);
                strWhere.Append("' ");
            }
            if (imgCombo_GongdiType.SelectedIndex != 0)
            {
                strWhere.Append(" and t.GongdiType ='");
                strWhere.Append(imgCombo_GongdiType.SelectedItem);
                strWhere.Append("' ");
            }
            if (imgCombo_LandStatus.SelectedIndex != 0)
            {
                strWhere.Append(" and t.LandStatus ='");
                strWhere.Append(imgCombo_LandStatus.SelectedItem);
                strWhere.Append("' ");
            }
            if (imgCombo_xzq.SelectedIndex != 0)
            {
                strWhere.Append(" and t.AdministratiVeregion ='");d
                strWhere.Append(imgCombo_xzq.SelectedItem);
                strWhere.Append("' ");
            }
            if (imgCombox_PlanedUse.SelectedIndex != 0)
            {
                strWhere.Append(" and t.PlanedUse ='");
                strWhere.Append(imgCombox_PlanedUse.SelectedItem);
                strWhere.Append("' ");
            }
            if (textEdit_StartingPriceE.Text != "" && textEdit_StartingPriceE.Text!="0")
            {
                strWhere.Append(" and StartingPrice between ");
                strWhere.Append(textEdit_StartingPriceS.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_StartingPriceE.Text);
            }
            if (textEdit_ConstructionLandAreaE.Text != "" && textEdit_ConstructionLandAreaE.Text != "0")
            {
                strWhere.Append(" and ConstructionLandArea between ");
                strWhere.Append(textEdit_ConstructionLandAreaS.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_ConstructionLandAreaE.Text);
            }
            if (textEdit_PlanningConstructionAreaE.Text != "" && textEdit_PlanningConstructionAreaE.Text != "0")
            {
                strWhere.Append(" and PlanningConstructionArea between ");
                strWhere.Append(textEdit_PlanningConstructionAreaS.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_PlanningConstructionAreaE.Text);
            }
            if (textEdit_PriceE.Text != "" && textEdit_PriceE.Text != "0")
            {
                strWhere.Append(" and Price between ");
                strWhere.Append(textEdit_PriceS.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_PriceE.Text);
            }

            if (textEdit_PriceOfASquareE.Text != "" && textEdit_PriceOfASquareE.Text != "0")
            {
                strWhere.Append(" and PriceOfASquare between ");
                strWhere.Append(textEdit_PriceOfASquareS.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_PriceOfASquareE.Text);
            }

            if (textEdit_PremiumRateE.Text != "" && textEdit_PremiumRateE.Text != "0")
            {
                strWhere.Append(" and PremiumRate between ");
                strWhere.Append(textEdit_PremiumRateS.Text);
                strWhere.Append(" and ");
                strWhere.Append(textEdit_PremiumRateE.Text);
            } 
            try
            {
                DataSet ds = _sqlClass.GetLandInfoList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
              //  GetResultData(ds.Tables[0]);

                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                    btn_select.Enabled = true;
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            }
        }

        private void GetResultData(DataTable dt)
        {
            //_dtResult.Clear();
            //float totalSgmj = 0;
            //float totalJgmj = 0;
            //foreach (DataRow row in dt.Rows)
            //{

            //    float sgmj = float.Parse(row["WorkArea"].ToString());
            //    float jgmj = float.Parse(row["OverArea"].ToString());
            //    totalSgmj += sgmj;
            //    totalJgmj += jgmj;
            //}
            //DataRow newDr = _dtResult.NewRow();
            //newDr["总施工面积（㎡）"] = totalSgmj;
            //newDr["总竣工面积（㎡）"] = totalJgmj;
            //_dtResult.Rows.Add(newDr);
            //BeginInvoke(new MethodInvoker(() =>
            //{
            //    gridControl1.DataSource = _dtResult;
            //    gridView1.GroupPanelText = "共计" + _dtResult.Rows.Count + "条数据";
            //}));
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            if (!CheckDateEdit())
            {
                return;
            }
            gridView2.GroupPanelText = "正在查询...";
            btn_select.Enabled = false;
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

        //private void textEdit_proName_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        btn_select_Click(null, null);
        //    }
        //}


        private bool CheckDateEdit()
        {

            if (dateEdit_CtimeStart.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_CtimeEnd.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_CtimeStart.DateTime > dateEdit_CtimeEnd.DateTime)
            {
                XtraMessageBox.Show("您选择的【公告时间】范围中的起始日期不能大于截止日期！");
                dateEdit_CtimeStart.Focus();
                return false;
            }
            if (dateEdit_StartTimeS.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_StartTimeE.DateTime > DateTime.Parse("1000/01/01")
                && dateEdit_StartTimeS.DateTime > dateEdit_StartTimeE.DateTime)
            {
                XtraMessageBox.Show("您选择的【起始时间】搜索范围中的起始日期不能大于截止日期！");
                dateEdit_StartTimeS.Focus();
                return false;
            }
            if (dateEdit_EndTimeS.DateTime > DateTime.Parse("1000/01/01")
               && dateEdit_EndTimeE.DateTime > DateTime.Parse("1000/01/01")
               && dateEdit_EndTimeS.DateTime > dateEdit_EndTimeE.DateTime)
            {
                XtraMessageBox.Show("您选择的【截止时间】搜索范围中的起始日期不能大于截止日期！");
                dateEdit_EndTimeS.Focus();
                return false;
            }
            if (textEdit_ConstructionLandAreaS.Text != "" && textEdit_ConstructionLandAreaE.Text!=""
                && int.Parse(textEdit_ConstructionLandAreaS.Text) > 
                int.Parse(textEdit_ConstructionLandAreaE.Text))
            {
                XtraMessageBox.Show("您选择的【建设用地面积】搜索范围中的起始值不能大于截止值！");
                textEdit_ConstructionLandAreaS.Focus();
                return false;
            }
            if (textEdit_PlanningConstructionAreaS.Text != "" && textEdit_PlanningConstructionAreaE.Text != ""
               && int.Parse(textEdit_PlanningConstructionAreaS.Text) >
               int.Parse(textEdit_PlanningConstructionAreaE.Text))
            {
                XtraMessageBox.Show("您选择的【规划建筑面积】搜索范围中的起始值不能大于截止值！");
                textEdit_ConstructionLandAreaS.Focus();
                return false;
            }
            if (textEdit_StartingPriceS.Text != "" && textEdit_StartingPriceE.Text != ""
             && int.Parse(textEdit_StartingPriceS.Text) > int.Parse(textEdit_StartingPriceE.Text))
            {
                XtraMessageBox.Show("您选择的【起始价】搜索范围中的起始值不能大于截止值！");
                textEdit_StartingPriceS.Focus();
                return false;
            }
            if (textEdit_PriceS.Text != "" && textEdit_PriceE.Text != ""
            && int.Parse(textEdit_PriceS.Text) > int.Parse(textEdit_PriceE.Text))
            {
                XtraMessageBox.Show("您选择的【成交价】搜索范围中的起始值不能大于截止值！");
                textEdit_PriceS.Focus();
                return false;
            }
            if (textEdit_PriceOfASquareS.Text != "" && textEdit_PriceOfASquareE.Text != ""
            && int.Parse(textEdit_PriceOfASquareS.Text) > int.Parse(textEdit_PriceOfASquareE.Text))
            {
                XtraMessageBox.Show("您选择的【楼面均价】搜索范围中的起始值不能大于截止值！");
                textEdit_PriceS.Focus();
                return false;
            }
            if (textEdit_PremiumRateS.Text != "" && textEdit_PremiumRateE.Text != ""
            && int.Parse(textEdit_PremiumRateS.Text) > int.Parse(textEdit_PremiumRateE.Text))
            {
                XtraMessageBox.Show("您选择的【溢价率】搜索范围中的起始值不能大于截止值！");
                textEdit_PremiumRateS.Focus();
                return false;
            }
            return true;
        }



        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "土地数据列表";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView2.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }


        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Right)
            //{
            //    return;
            //}
            //string fileName = "施工数据统计";
            //saveFileDialog1.FileName = fileName;
            //saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    gridView1.ExportToXls(saveFileDialog1.FileName);
            //    XtraMessageBox.Show("Excel导出完成！");
            //}
        }

        private void textEdit_Leave(object sender, EventArgs e)
        {
            TextEdit textEdit = sender as TextEdit;
            if (textEdit!=null && textEdit.Text=="")
            {
                textEdit.Text = "";
            }
        }
         

    }
}